package backend.attribute.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import backend.attribute.manager.AttributeManager;
import backend.attribute.manager.AttributeSetManager;
import backend.attribute.manager.AttributeValueManager;
import backend.paging.Paging;
import com.opensymphony.xwork2.ActionSupport;
import core.attribute.model.Attribute;
import core.attribute.model.AttributeSet;
import core.attribute.model.AttributeValue;

public class AttributeSetController extends ActionSupport 
{
	private static final long serialVersionUID = 123L;
	private AttributeSetManager asm ;
	private AttributeManager am ;
	private AttributeValueManager avm ;
	private List<AttributeSet> attributeSets ;
	private List<Attribute> attributes ;
	private Attribute attribute ;
	private AttributeSet attributeSet ;
	// attribute used for paging (pageSize,p and totalPage)
	private int pageSize ; // default value 10
	private int p ;
	private int totalPage ;
	private List<String> pageHtml ;
	private String msg ;
	private String errorMsg ;
	private String backUrl ;
	private Long uuid ;
	private Long attributeUuid ;
	
	public AttributeSetController()
	{
		asm = new AttributeSetManager();
		am = new AttributeManager();
		avm = new AttributeValueManager();
	}
	
	public String execute() 
	{
		List<AttributeSet> attrFullLst = asm.getAttributeSetList() ;
		if(this.pageSize == 0)
		{
			this.setPageSize(10);
		}
		Paging<AttributeSet> paging = new Paging<AttributeSet>();
		this.setPageHtml(paging.getPageHtml(this.getP(), pageSize, attrFullLst.size()));
		this.setAttributeSets(paging.getPageList(this.pageSize,attrFullLst , this.p));
		return SUCCESS ;
	}
	
	public String updateAttributeSet()
	{
		if(this.pageSize == 0)
		{
			this.setPageSize(10);
		}
		Paging<AttributeSet> paging = new Paging<AttributeSet>();
		if(asm.getAttributeSetByName(this.getAttributeSet().getName()) == null)
		{
			asm.saveAttributeSet(this.getAttributeSet());
			this.setMsg("Attribute Set saved Successfully");
			this.setAttributeSets(paging.getPageList(this.pageSize, asm.getAttributeSetList(), this.p));
			return SUCCESS ;
		}
		this.setErrorMsg("Could not save Attribute Set.Name Allready Exists");
		this.setAttributeSets(paging.getPageList(this.pageSize, asm.getAttributeSetList(), this.p));
		return ERROR ;
	}
	public String deleteAttributeSet()
	{
		if(this.pageSize == 0)
		{
			this.setPageSize(10);
		}
		AttributeSet as = asm.getAttributeSetByUuid(this.getUuid());
		Paging<AttributeSet> paging = new Paging<AttributeSet>();
		this.setAttributeSet(as);
		Iterator<Attribute> attrs = as.getAttributes().iterator();
		as.setAttributes(null);
		asm.saveAttributeSet(as);
		while(attrs.hasNext())
		{
			Attribute a = attrs.next();
			if(a.isMultivalued())
			{
				Iterator<AttributeValue> avItr = a.getValues().iterator();
				a.setValues(null);
				am.save(a);
				while(avItr.hasNext())
				{
					AttributeValue av = avItr.next();
					avm.delete(av);
				}
			}
			am.remove(a);
		}
		asm.deleteAttributeSet(as);
		this.setAttributeSets(paging.getPageList(this.pageSize, asm.getAttributeSetList(), this.p));
		return SUCCESS ;
	}
	public String editAttributeSet()
	{
		if(this.pageSize == 0)
		{
			this.setPageSize(10);
		}
		this.setAttributeSet(asm.getAttributeSetByUuid(this.getUuid()));
		List<Attribute> atbL = new ArrayList<Attribute>();
		atbL.addAll(this.getAttributeSet().getAttributes());
		Paging<Attribute> paging = new Paging<Attribute>();
		this.setPageHtml(paging.getPageHtml(this.getP(), this.pageSize, atbL.size()));
		this.setAttributes(paging.getPageList(this.pageSize,atbL , this.getP()));
		this.setBackUrl("attributeSet?p="+this.getP());
		return SUCCESS ;
	}
	public String createAttribute()
	{
		AttributeSet as = asm.getAttributeSetByUuid(this.getUuid()) ;
		as.getAttributes().add(this.getAttribute());
		asm.saveAttributeSet(as);
		this.setAttributeSet(as);
		List<Attribute> atbL = new ArrayList<Attribute>();
		atbL.addAll(as.getAttributes());
		this.setAttributes(atbL);
		return SUCCESS ; 
	}
	public String deleteAttribute()
	{
		AttributeSet as = asm.getAttributeSetByUuid(this.getUuid()) ;
		Attribute a = am.getAttributeByUuid(this.getAttributeUuid());
		Set<Attribute> atbL = new HashSet<Attribute>();
		Iterator<Attribute> oldItr = as.getAttributes().iterator();
		while(oldItr.hasNext())
		{
			Attribute localAttr = oldItr.next();
			if(!a.getUuid().equals(localAttr.getUuid()))
			{
				atbL.add(localAttr);
			}
		}
		as.setAttributes(atbL);
		asm.saveAttributeSet(as);
		List<Attribute> newLst = new ArrayList<Attribute>();
		newLst.addAll(atbL);
		// remove values if multivalued
		if(a.isMultivalued())
		{
			Iterator<AttributeValue> avItr = a.getValues().iterator();
			a.setValues(null);
			am.save(a);
			while(avItr.hasNext())
			{
				AttributeValue av = avItr.next();
				avm.delete(av);
			}
		}
		am.remove(a);
		this.setAttributes(newLst);
		this.setAttributeSet(as);
		return SUCCESS ; 
	}
	public List<AttributeSet> getAttributeSets() {
		return attributeSets;
	}

	public void setAttributeSets(List<AttributeSet> attributeSets) {
		this.attributeSets = attributeSets;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getP() {
		return p;
	}

	public void setP(int p) {
		this.p = p;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public AttributeSet getAttributeSet() {
		return attributeSet;
	}

	public void setAttributeSet(AttributeSet attributeSet) {
		this.attributeSet = attributeSet;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public String getBackUrl() {
		return backUrl;
	}

	public void setBackUrl(String backUrl) {
		this.backUrl = backUrl;
	}

	public Long getUuid() {
		return uuid;
	}

	public void setUuid(Long uuid) {
		this.uuid = uuid;
	}

	public List<Attribute> getAttributes() {
		return attributes;
	}

	public void setAttributes(List<Attribute> attributes) {
		this.attributes = attributes;
	}

	public Attribute getAttribute() {
		return attribute;
	}

	public void setAttribute(Attribute attribute) {
		this.attribute = attribute;
	}

	public List<String> getPageHtml() {
		return pageHtml;
	}

	public void setPageHtml(List<String> pageHtml) {
		this.pageHtml = pageHtml;
	}

	public Long getAttributeUuid() {
		return attributeUuid;
	}

	public void setAttributeUuid(Long attributeUuid) {
		this.attributeUuid = attributeUuid;
	}

}
